<template>
  <div style="width: 100%">
    <div style="float: left">
      <h1>密码重置</h1>
    </div>
    <div class="formBlock" >
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">

                <el-form-item label="密码" prop="password" >
                  <el-input v-model="formData.password" placeholder="请输入密码"  clearable :style="{width: '100%'}" size="mini">
                  </el-input>
                </el-form-item>


        <el-form-item size="small" style="margin-left: 250px">
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="resetForm">重置</el-button>
          <el-button @click="goBack">返回</el-button>
        </el-form-item>
      </el-form>
    </div>

  </div>

</template>
<script>
import axios from 'axios'
export default {
  components: {},
  props: [],
  data() {
    return {

      courseOption: [],
      chooseCourse: [],
      oldAccount: '',
      oldPassword: '',
      formData: {
        sNo: sessionStorage.getItem('sno'),
        password: '',
      },
      rules: {
        password: [{
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        }, {
          pattern: /\S/,
          message: '密码不能为空',
          trigger: 'blur'
        }],


      },
    }
  },
  computed: {},
  watch: {},
  // created() {
  //   var sno =sessionStorage.getItem('sno')
  //   // alert(sno)  //弹框提示学号信息
  //   var _this =this
  //   axios.get('http://localhost:8080/jas/sport/student/getStuBySnoWith/'+sno).then(function (resp) {
  //     console.log(resp.data)
  //     _this.formData = resp.data
  //     _this.oldPassword = resp.data.password
  //   })
  //
  // },
  mounted() {

  },

  methods: {
    submitForm() {
      const _this = this
      this.$refs['elForm'].validate(valid => {
        if (!valid) return
        // TODO 提交表单
        axios.post('http://localhost:8080/jas/sport/student/updatePassword', this.formData).then(function (resp) {
          if (resp.data === 'success') {
            alert("提交成功")
          }else{
            alert("提交失败")
          }

          if(_this.name != _this.formData.name)
          {
            _this.name = sessionStorage.setItem('stuName',_this.formData.name);
          }

          // 账号或者密码被修改后，跳转到登录页面
          if (_this.oldPassword != _this.formData.password) {
            sessionStorage.clear()
            _this.$router.push('/StudentLogin')
          }
        })




      })
    },
    resetForm() {
      this.$refs['elForm'].resetFields()
    },
    goBack(){
      this.$router.push('/StudentMainPage')
    }
  }
}

</script>
<style lang="scss"  scoped>
.formBlock{
  width: 50%;
  margin-left: 150px;
  float: left;
  margin-top: 110px;
}
</style>
